package com.example.leo.mycharmingstudent.a0917.practice;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.leo.mycharmingstudent.R;

import java.util.List;

/**
 * Created by Leo on 2020/9/17.
 */

public class BannerAdapter extends PagerAdapter {

    private List<BannerEntity.DataBean> mList;
    private Context mContext;

    public BannerAdapter(List<BannerEntity.DataBean> list, Context context) {
        this.mList = list;
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_vp_banner_adapter, null, false);
        ImageView ivBanner = inflate.findViewById(R.id.iv_banner_image_vp_banner_adapter);
        String imagePath = mList.get(position).getImagePath();
        Glide.with(mContext).load(imagePath).into(ivBanner);

        //不执行此方法，界面不展示
        container.addView(inflate);

        return inflate;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }
}
